﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace 机器组和机器编辑
{
    public partial class EditForm : Form
    {
        List<MachineGroup> machineGroups;

        public EditForm()
        {
            InitializeComponent();
            MachineGroup group = new MachineGroup();
        }

        public void ModifyMachineGroupById( int GroupID) //修改实现
        {
            if (machineGroups == null)
            {
                // 这里从XML加载所有MachineGroup
                machineGroups = LoadMachineGroupsFromXml();
            }

            MachineGroup group = machineGroups.FirstOrDefault(x => x.GroupID == GroupID);

            if (group != null)
            {
                textBox5.Text = group.GroupID.ToString(); //ID
                textBox1.Text = group.GroupName;  //名称
                checkBox1.Checked = group.GroupConfiguration; //布尔类型用Checked
                comboBox2.Text = group.Language;
                comboBox1.Text = group.GroupType;
            }
            else
            {
                MessageBox.Show("未找到对应的机器组数据！");
            }
        }
        // 从XML加载
        private List<MachineGroup> LoadMachineGroupsFromXml()
        {
            var list = new List<MachineGroup>();
            var doc = XDocument.Load("C:\\Users\\24929\\Desktop\\机器组和机器编辑\\Config\\MachineGroupConfiguration.xml");
            foreach (var xe in doc.Root.Elements("MachineGroup"))
            {
                list.Add(MachineGroup.FromXElement(xe));
            }
            return list;
        }

        private void Form4_Load(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e) //ID
        {

        }

        private void button2_Click(object sender, EventArgs e) //取消
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e) //确定
        {
            if (machineGroups == null)
                machineGroups = LoadMachineGroupsFromXml();

            int groupId = int.Parse(textBox5.Text);
            MachineGroup group = machineGroups.FirstOrDefault(x => x.GroupID == groupId);
            if (group != null)
            {
                group.GroupName = textBox1.Text;
                group.GroupConfiguration = checkBox1.Checked;
                group.Language = comboBox2.Text;
                group.GroupType = comboBox1.Text;
                SaveMachineGroupsToXml(machineGroups);
                MessageBox.Show("保存成功！");
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("未找到对应的机器组数据！");
            }
        }

        //保存到 XML 文件
        private void SaveMachineGroupsToXml(List<MachineGroup> groups)
        {
            var doc = new XDocument(
                new XElement("MachineGroups",
                    groups.Select(g => g.ToElement())
                )
            );
            doc.Save("C:\\Users\\24929\\Desktop\\机器组和机器编辑\\Config\\MachineGroupConfiguration.xml");
        }
    }
}
